package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookTagMapper;
import com.kgc.novel.domain.entity.BookTag;
import com.kgc.novel.domain.param.BookTagParam;
import com.kgc.novel.service.book.BookTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookTagServiceImpl extends ServiceImpl<BookTagMapper, BookTag> implements BookTagService {

    @Resource
    private BookTagMapper bookTagMapper;

    @Override
    public Res selectBookTagAll(BookTagParam bookTagParam) {
        Objects.nonNull(bookTagParam);

        LambdaQueryWrapper<BookTag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(bookTagParam.getTagName()),BookTag::getTagName,bookTagParam.getTagName());
        Page<BookTag> page = new Page<>(bookTagParam.getPageNo(), bookTagParam.getPageSize());
        Page<BookTag> bookTagPage= bookTagMapper.selectPage(page, lambdaQueryWrapper);
        return Res.returnData(bookTagPage,bookTagPage.getTotal());
    }

    @Override
    public Res selectBookTagAll() {
        LambdaQueryWrapper<BookTag> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        List<BookTag> bookTags = bookTagMapper.selectList(lambdaQueryWrapper);
        return Res.returnData(bookTags,(long)bookTags.size());
    }
}
